package com.example.memorycardgame;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.example.memorycardgame.adapter.CardAdapter;
import com.example.memorycardgame.model.CardMatchingGame;

public class CardGameActivity extends AppCompatActivity implements CardAdapter.OnCardClickListener {
    private RecyclerView rvCards;
    private TextView tvScore, tvHighScore;
    private Button btnRestart;
    private CardMatchingGame game;
    private int gameHighScore;
    private int cardCount;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_card_game);

        // 初始化控件
        rvCards = findViewById(R.id.rv_cards);
        tvScore = findViewById(R.id.tv_score);
        tvHighScore = findViewById(R.id.tv_high_score);
        btnRestart = findViewById(R.id.btn_restart);

        // 获取主界面传递的最高分
        gameHighScore = getIntent().getIntExtra("currentHighScore", 0);
        tvHighScore.setText("最高分：" + gameHighScore);

        // 根据屏幕方向设置卡片数量（竖屏24张，横屏36张，示例）
        if (getResources().getConfiguration().orientation == android.content.res.Configuration.ORIENTATION_PORTRAIT) {
            cardCount = 24;
        } else {
            cardCount = 36; // 横屏增加卡片数
        }

        // 初始化游戏
        game = new CardMatchingGame(cardCount);
        setupRecyclerView();

        // 绑定事件
        btnRestart.setOnClickListener(v -> restartGame());
    }

    private void setupRecyclerView() {
        int spanCount = getResources().getConfiguration().orientation == android.content.res.Configuration.ORIENTATION_PORTRAIT ? 4 : 6;
        rvCards.setLayoutManager(new GridLayoutManager(this, spanCount));
        rvCards.setAdapter(new CardAdapter(game.getCards(), this));
        updateScoreDisplay();
    }

    @Override
    public void onCardClick(int position) {
        game.chooseCardAtIndex(position);
        rvCards.getAdapter().notifyItemChanged(position);
        updateScoreDisplay();

        // 检查游戏是否结束
        boolean isGameOver = true;
        for (int i = 0; i < game.getCards().size(); i++) {
            if (!game.getCards().get(i).isMatched()) {
                isGameOver = false;
                break;
            }
        }
        if (isGameOver) {
            if (game.getScore() > gameHighScore) {
                gameHighScore = game.getScore();
                tvHighScore.setText("最高分：" + gameHighScore);
                Intent resultIntent = new Intent();
                resultIntent.putExtra("highScore", gameHighScore);
                setResult(RESULT_OK, resultIntent);
            }
        }
    }

    private void updateScoreDisplay() {
        tvScore.setText("当前得分：" + game.getScore());
    }

    private void restartGame() {
        game.reset();
        rvCards.getAdapter().notifyDataSetChanged();
        updateScoreDisplay();
    }
}